// hyb_yl/zhuanjiasubpages/pages/bloodpressure_report/bloodpressure_report.js
var wxCharts = require("../../../../utils/wxcharts.js");
var app = getApp();
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
var getTimes = i => {
  var last = new Date();
  const year = last.getFullYear()
  const day = last.getDate()
  const ti = day - i
  // 判断是否月初
  if (ti <= 0) {
    const month = last.getMonth() + 1 - 1
    const d = new Date(year, month, 0)
    const dayBig = d.getDate() //获取当月的所有天数
    const ti1 = dayBig + ti
    return [year, month, ti1].map(formatNumber).join('-')
  } else {
    const month = last.getMonth() + 1
    return [year, month, ti].map(formatNumber).join('-')
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabindex: 0,
    tablist: ['总览','趋势' ,'数据' ,'用药'],
    array:["餐前",'餐后'],
    index:0,
    time_start:'2021-03-27',
    time_end:'2021-03-27',
    type:"",
    style:"",
  },
  bindtab(e){
    this.setData({
      tabindex:e.currentTarget.dataset.index,
      type:e.currentTarget.dataset.index,
      data:[]
    })
    this.getUser_sugar();
  },
  binddetail(e){
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/service_notice/service_notice',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var openid = options.openid;
    var j_id = options.j_id;
    that.setData({
      openid: openid,
      j_id: j_id
    })
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var time_end = Y + "-" + M + "-" + D;
    var time_start = getTimes(7)
    that.setData({
      time_start: time_start,
      time_end: time_end
    })
    that.getUser_sugar();
  },
  // 获取用户血糖记录
  getUser_sugar:function(e){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/index.user_sugar",
      data:{
        openid:wx.getStorageSync('openid'),
        j_id: that.data.j_id,
        type: that.data.type,
        style: that.data.style,
        start: that.data.time_start,
        end: that.data.time_end
      },
      success:function(res){
        that.setData({
          data:res.data
        })
        if(that.data.type == 0 || that.data.type == '')
        {
          var data = res.data;
          var before_normal_sugar = data.before_normal_sugar;
          var before_height_sugar = data.before_height_sugar;
          var before_low_sugar = data.before_low_sugar;
          var before_normal_sugar_bili = data.before_normal_sugar_bili;
          var before_height_sugar_bili = data.before_height_sugar_bili;
          var before_low_sugar_bili = data.before_low_sugar_bili;
          var after_normal_sugar = data.after_normal_sugar;
          var after_height_sugar = data.after_height_sugar;
          var after_low_sugar = data.after_low_sugar;
          var after_normal_sugar_bili = data.after_normal_sugar_bili;
          var after_height_sugar_bili = data.after_height_sugar_bili;
          var after_low_sugar_bili = data.after_low_sugar_bili;
          var normal = data.normal;
          var height = data.height;
          var low = data.low;
          var normal_bili = data.normal_bili;
          var height_bili = data.height_bili;
          var low_bili = data.low_bili;
          console.log(before_normal_sugar,before_height_sugar,before_low_sugar)
          console.log(normal,height,low)
          // 总血糖记录
          try {
            var res = wx.getSystemInfoSync();
            var windowWidth = res.windowWidth;
            var windowHeight = res.windowHeight * 0.3;
          } catch (e) {
            console.error("系统信息错误")
          };
          new wxCharts({
            canvasId: 'ringCanvas',
            animation: true,
            type: 'ring',
            title:{
              name: '总血糖',
              color: '#333333',
              fontSize: 14
            },
            series: [{
                name: '正常：'+normal+'次'+normal_bili+'% ',
                data: normal,
                color:'#0087e0',
      
            }, {
                name: '高血糖：'+height+'次'+height_bili+'% ',
                data: height,
                color:'#ff7272',
            }, {
                name: '低血糖：'+low+'次'+low_bili+'% ',
                data: low,
                color:'#4dbb99',
            }],
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            disablePieStroke: true,
            extra:{
              pie: {
                offsetAngle: 90
              },
              ringWidth: 15,
            }
          })
          try {
            var res = wx.getSystemInfoSync();
            var windowWidth = res.windowWidth/2;
            var windowHeight = res.windowHeight * 0.3;
          } catch (e) {
            console.error("系统信息错误")
          };
         
          new wxCharts({
            canvasId: 'ringCanvas1',
            animation: true,
            type: 'ring',
            title:{
              name: '餐前',
              color: '#333333',
              fontSize: 12
            },
            series: [{
                name: '正常：'+before_normal_sugar+'次'+before_normal_sugar_bili+'% ',
                data: before_normal_sugar,
                color:'#0087e0',
                fontSize:10,
      
            }, {
                name: '高血糖：'+before_height_sugar+'次'+before_height_sugar_bili+'% ',
                data: before_height_sugar,
                color:'#ff7272',
                fontSize:10,
      
            }, {
                name: '低血糖：'+before_low_sugar+'次'+before_low_sugar_bili+'% ',
                data: before_low_sugar,
                color:'#4dbb99',
                fontSize:10,
            }],
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            disablePieStroke: true,
            extra:{
              pie: {
                offsetAngle: 90
              },
              ringWidth: 12,
            }
          })
          new wxCharts({
            canvasId: 'ringCanvas2',
            animation: true,
            type: 'ring',
            title:{
              name: '餐后',
              color: '#333333',
              fontSize: 12
            },
            series: [{
                name: '正常：'+after_normal_sugar+'次'+after_normal_sugar_bili+'% ',
                data: after_normal_sugar,
                color:'#0087e0',
                fontSize:10,
      
            }, {
                name: '高血糖：'+after_height_sugar+'次'+after_height_sugar_bili+'% ',
                data: after_height_sugar,
                color:'#ff7272',
                fontSize:10,
      
            }, {
                name: '低血糖：'+after_low_sugar+'次'+after_low_sugar_bili+'% ',
                data: after_low_sugar,
                color:'#4dbb99',
                fontSize:10,
            }],
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            disablePieStroke: true,
            extra:{
              pie: {
                offsetAngle: 90
              },
              ringWidth: 12,
            }
          })
        }else if(that.data.type == 1)
        {
          var date = res.data.date;
          var sugar = res.data.sugar;
          try {
            var res = wx.getSystemInfoSync();
            var windowWidth = res.windowWidth;
            var windowHeight = res.windowHeight * 0.3;
          } catch (e) {
            console.error("系统信息错误")
          };
          new wxCharts({ //当月用电折线图配置
            canvasId: 'lineCanvas',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '血糖值',
              data: sugar,
              color:'#ff7272',
              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 20,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
        }
      }
    })
  },
  // 开始日期
  bindDateStart(e){
    console.log("开始日期",e)
    this.setData({
      time_start:e.detail.value
    })
    this.getUser_sugar();
  },
  // 选择类型
  bindtype:function(e){
    var that = this;
    that.setData({
      index:e.detail.value,
      style:e.detail.value,
    })
    that.getUser_sugar();
  },
  // 结束日期
  bindDateEnd(e){
    console.log("结束日期",e)
    this.setData({
      time_end:e.detail.value
    })
    this.getUser_sugar();
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})